//
//  CYPassWord.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPassWordView.h"
#import "NSString+category.h"
#import "CYPassWordInput.h"
@interface CYPassWordView()<UITextFieldDelegate>{
    passwordBlock _myblock;
}
@property (nonatomic, strong) CYButton *cancelBtn;
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYView *line;
@property (nonatomic, strong) CYLabel *moneyLBL;
@property (nonatomic, strong) CYPassWordInput *passwordInput;
@property (nonatomic, strong) CYImageView *loadingIMG;  //检测转圈图
@property (nonatomic, strong) CYLabel *loadingLBL;     //校验文本框
@end
@implementation CYPassWordView
- (instancetype)initWithColor:(UIColor *)color block:(passwordBlock)callback{
    self = [super initWithColor:color];
    if (self) {
        _myblock = callback;
        self.cancelBtn.hidden = self.titleLBL.hidden = self.line.hidden = NO;
    }
    return self;
}

- (CYButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn =[[CYButton alloc]initWithImage:@"delete" cornerRadius:0.0];
        [_cancelBtn addTarget:self action:@selector(actionForCancelBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelBtn];
        
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.left.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(AL(PT(86)), AL(PT(86))));
        }];
        
    }
    return _cancelBtn;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"输入密码" size:18 textcolor:ColorHex(0x333333)];
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).with.offset((AL(PT(86)) - _titleLBL.bounds.size.height) / 2.0);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:ColorHex(0xe2dfdf)];
        [self addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AL(PT(86)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
    }
    return _line;
}

- (CYLabel *)moneyLBL{
    if (!_moneyLBL) {
        _moneyLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"提现金额%@元",_money] size:14 textcolor:ColorHex(0x888888)];
        [self addSubview:_moneyLBL];
        
        WS(self)
        [_moneyLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line.mas_bottom).with.offset(AL(PT(24)));
            make.centerX.equalTo(weakSelf);
            make.size.mas_equalTo(_moneyLBL.bounds.size);
        }];
    }
    return _moneyLBL;
}

- (CYPassWordInput *)passwordInput{
    if (!_passwordInput) {
        WS(self)
        _passwordInput = [[CYPassWordInput alloc]initWithLineColor:ColorHex(0x888888) backgroundColor:[UIColor whiteColor] textNumber:6 size:CGSizeMake(AL(PT(570)), AL(PT(95))) complete:^(NSMutableString *string) {
            [weakSelf check:(NSString *)string];
        }];
        [self addSubview:_passwordInput];
        
        [_passwordInput mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_moneyLBL.mas_bottom).with.offset(AL(PT(40)));
            make.centerX.equalTo(_moneyLBL);
            make.size.mas_equalTo(_passwordInput.bounds.size);
        }];
    }
    return _passwordInput;
}

- (CYImageView *)loadingIMG{
    if (!_loadingIMG) {
        _loadingIMG = [[CYImageView alloc]initWithImageName:@"loading" size:CGSizeMake(AL(PT(100)), AL(PT(100))) isRadius:NO];
        [self addSubview:_loadingIMG];
        
        WS(self)
        [_loadingIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf);
            make.top.equalTo(_line.mas_bottom).with.offset(AL(PT(196)));
            make.size.mas_equalTo(_loadingIMG.bounds.size);
        }];
    }
    return _loadingIMG;
}

- (CYLabel *)loadingLBL{
    if (!_loadingLBL) {
        _loadingLBL = [[CYLabel alloc]initWithText:@"正在校验" size:AL(PT(32)) textcolor:ColorHex(0x333333)];
        [self addSubview:_loadingLBL];
        
        [_loadingLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_loadingIMG);
            make.top.equalTo(_loadingIMG.mas_bottom).with.offset(AL(PT(40)));
            make.size.mas_equalTo(_loadingLBL.bounds.size);
        }];
    }
    return _loadingLBL;
}

- (void)setMoney:(NSString *)money{
    if(_money != money)
        _money = money;
    self.moneyLBL.text = [NSString stringWithFormat:@"提现金额%@元",_money];
    self.passwordInput.hidden = NO;
    [self updateMoneyLBL];
    
}

- (void)setTransform:(CGAffineTransform)transform{
    [super setTransform:transform];
    [self reInput];
}
//密码输入完成，开始校验
- (void)check:(NSString *)string{
    [_passwordInput resignFirstResponder]; //放弃第一响应
    self.loadingIMG.hidden = self.loadingLBL.hidden = NO;
    _myblock(string);
}
//取消按钮
- (void)actionForCancelBtn{
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
    [_passwordInput resignFirstResponder];
    _myblock(nil);
}
//显示自己 并且让passwordinpu变成第一响应对象
- (void)show{
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, -AUTO(335));
    }];
}

- (void)hidden{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
    [_passwordInput resignFirstResponder];
}

//重新输入
- (void)reInput{
    
    [_passwordInput reInput];
    _loadingIMG.hidden = _loadingLBL.hidden = YES;
    [_passwordInput becomeFirstResponder];
    
}

- (void)updateMoneyLBL{

[_moneyLBL mas_updateConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(_moneyLBL.bounds.size);
}];

}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
